package com.glela.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.glela.goods.mapper.GoodsCommentMapper;
import com.glela.goods.model.GoodsComment;
import com.glela.goods.model.request.GoodsCommentRequest;
import com.glela.goods.model.request.RequestGoodsCommenntBaseModel;
import com.glela.goods.model.vo.goods.GoodsCommentVo;
import com.glela.order.mapper.OrderDetailMapper;
import com.glela.order.mapper.OrderInfoMapper;
import com.glela.order.mapper.OrderObjectMapper;
import com.glela.order.model.OrderDetail;
import com.glela.order.model.OrderInfo;
import com.glela.order.model.response.ResponseCommentList;
import com.glela.order.service.GoodsCommentService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponsePageModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.talent.service.UserDailyLogService;

@Service("goodsComment")
@Scope("singleton")
public class GoodsCommentServiceImpl implements GoodsCommentService {

	@Autowired
	private GoodsCommentMapper goodsCommentMapper ;
	
	@Autowired
	private UserMapper userMapper ;
	
	/** MaoZeYao **/
	@Autowired
	private OrderDetailMapper orderDetailMapper;

	@Autowired
	private OrderObjectMapper orderObjectMapper;
	
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	
	@Autowired
	private UserDailyLogService userDailyLogService;
	/** MaoZeYao **/
	
	@Override
	public ResponsePageModel<GoodsCommentVo> listGoodsCommentByGoodId(GoodsCommentRequest request) {
		// TODO 自动生成的方法存根
		//商品评论个数总数
		int count = goodsCommentMapper.selectGoodsCommentByGoodIdCount(request.getGoodId());
		List<GoodsCommentVo> voList = new ArrayList<>();
		//查询商品评论分页根据商品id
		List<GoodsComment> commentList = goodsCommentMapper.selectGoodsCommentByGoodId(request.getStartIndex(), 
				request.getPageSize(), request.getGoodId()) ;
		//将返回的po对象循环转换为  vo对象   并且查询出对应的  评论用户信息  包括名字和 头像
		for (GoodsComment goodsComment : commentList) {
			User user = userMapper.selectUserEntityById(goodsComment.getUserId());
			if (user != null) {
				GoodsCommentVo vo = new GoodsCommentVo(goodsComment);
				vo.setNickName(user.getNickName());
				vo.setPicture(user.getPicture());
				voList.add(vo);
			}
		}
		return new ResponsePageModel<GoodsCommentVo>(count, request.getPageSize(), request.getPageNum(), voList);
	}

	/**
	 * 批量增加商品评论
	 * @throws Exception 
	 */
	@Override
	public ResponseEntity<?> increaseTheCommodityComments(Long userId,Long orderInfoId, List<RequestGoodsCommenntBaseModel> models) throws Exception {
		// TODO Auto-generated method stub
		OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderInfoId);
		
		// 处理评论内容
		for(RequestGoodsCommenntBaseModel model : models){
			// 增加商品评论
			OrderDetail orderDetail = orderDetailMapper.selectOrderDetailEntityBuId(model.getOrderDetailId());
			if(orderDetail.getCommentStatus() == 0){
				int insertStatus = goodsCommentMapper.insertGoodsComment(model.getGoodId(), userId, model.getCommentText(), model.getImage1(),
						model.getImage2(), model.getImage3(), model.getSpecName(),model.getOrderDetailId());
				if(insertStatus <= 0 ){
					throw new Exception();
				}
				// 修改订单详情的评论状态
				int updateStatus = orderDetailMapper.updateOrderDetailToCommentSuccess(model.getOrderDetailId());
				if (updateStatus <= 0) {
					throw new Exception();
				}
				// 增加经验值
				userDailyLogService.addUserDailyLog(userId, 4);
			}
		}
		
		// 处理子订单
		int orderDetailCount = orderDetailMapper.selectOrderDetailByOrderInfoId(orderInfoId);
		int orderDetailCompliteCount = orderDetailMapper.selectOrderDetailCommentCompliteByOrderInfoId(orderInfoId);
		if(orderDetailCount == orderDetailCompliteCount){
			int updateStatus = orderInfoMapper.updateOrderInfoToComment(orderInfoId);
			if (updateStatus <= 0) {
				throw new Exception();
			}
		}
		
		// 处理父订单
		int orderInfoCount = orderInfoMapper.selectOrderInfoByOrderObjectId(orderInfo.getOrderObjectId());
		int orderInfoCompliteCount = orderInfoMapper.selectOrderInfoCommentCompliteByOrderObjectId(orderInfo.getOrderObjectId());
		if(orderInfoCount == orderInfoCompliteCount){
			int updateStatus = orderObjectMapper.updateOrderObjectToCommentSuccess(orderInfo.getOrderObjectId());
			if (updateStatus <= 0) {
				throw new Exception();
			}
		}
		return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
	}

	@Override
	public ResponseEntity<?> findCanCommentOrderList(Long userId, Long orderInfoId) {
		// TODO Auto-generated method stub
		List<OrderDetail> detailList = orderDetailMapper.selectOrderDetailEntityByOrderInfoId(orderInfoId);
		if(detailList == null ){
			List<ResponseCommentList> resList = new ArrayList<ResponseCommentList>();
			return new ResponseEntity<>(new ResponseResultModel<>(resList), HttpStatus.OK);
		}
		//if(detailList.get(0).getUserId() != userId){
		if(!detailList.get(0).getUserId().equals(userId)){
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20154), HttpStatus.OK);
		}
		List<ResponseCommentList> resList = new ArrayList<ResponseCommentList>();
		for (OrderDetail detail : detailList) {
			if(detail.getCommentStatus() == 1)continue;
			if(detail.getStatus() == 4){
				ResponseCommentList resModel = new ResponseCommentList();
				resModel.setGoodsId(detail.getGoodId());
				resModel.setOrderDetailId(detail.getId());
				resModel.setGoodName(detail.getGoodName());
				resModel.setSalePrice(detail.getSalePrice());
				resModel.setQuantity(detail.getQuantity());
				resModel.setSpecName(detail.getSpecName());
				resModel.setNavigateImg1(detail.getNavigateImg1());
				resList.add(resModel);
			}
		}
		return new ResponseEntity<>(new ResponseResultModel<>(resList), HttpStatus.OK);
	}

}
